/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.composite.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.Port;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.GraphNodeWithSemanticEditPart;
import com.cea.papyrus.core.editpart.connection.AnchorMngr;
import com.cea.papyrus.core.editpart.connection.EllipseAnchorMngr;
import com.cea.papyrus.core.figures.ElementFigure;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.composite.figure.ProvidedInterfaceFigure;
import com.cea.papyrus.diagram.composite.figure.RequiredInterfaceFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphConnector;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Controler of  an {@link Interface} in the composite diagram
 * its representation is a circle or a half circle.
 */
public class InterfaceEditPart extends GraphNodeWithSemanticEditPart {

	/**
	 * Creates a new InterfaceEditPart.
	 * @param graphNode the graph node representing the interface
	 * @param modelManager the model manager associated to the editor
	 */
	public InterfaceEditPart(GraphNode graphNode,ModelManager modelManager) {
		super(graphNode,modelManager);
	}

    /**
     * Install diagram specific editPolicy
     */
    @Override
    protected void createEditPolicies(){
        super.createEditPolicies();
        // overload the GRAPHICAL_NODE_ROLE editPolicy to use the diagram specific one
        installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new GraphNodeEditPolicy() );
    }

    /* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#getNodeFigure()
	 */
	@Override
	protected ElementFigure getNodeFigure(){
		return (ElementFigure)createFigure() ;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.connections.editPart.AnchorableGraphNodeEditPart#createAnchorManager()
	 */
	@Override
	protected AnchorMngr createAnchorManager() {
		// special anchor manager for interfaceEditParts.
		return new EllipseAnchorMngr(this);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#createFigure()
	 */
	@Override
	protected IFigure createFigure() {
		// Try to get figure from ContainerWithSemanticEditPart
		IFigure fig = super.createFigure();
		if(((GraphNode)getGraphNode().getSemanticParent())!=null){

			Port port=(Port)((Uml1SemanticModelBridge)((GraphNode)getGraphNode().getSemanticParent()).getSemanticModel()).getElement();

			if (fig != null) {
				return fig;
			}
			if (port.getRequireds().contains(getUmlElement())){
				return new RequiredInterfaceFigure();
			}
			else{
				return new ProvidedInterfaceFigure();
			}
		}
		// else
		return new ProvidedInterfaceFigure();

	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#activate()
	 */
	@Override
	public void activate() {
		if (isActive()) {
			Debug.debug(this, "should not activate an already active edit part", channel);
			return;
		}
		super.activate();
		if(getGraphNode().getAnchorage().size()!=0){
			((Notifier)(GraphConnector)getGraphNode().getAnchorage().get(0)).eAdapters().add(graphElementListener);
		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#deactivate()
	 */
	@Override
	public void deactivate() {
		if (!isActive()) {
			Debug.debug(this, "should not deactivate an already deactived edit part", channel);
			return;
		}
		// stop listening for changes in the model
		if(getGraphNode().getAnchorage().size()!=0){
			((Notifier)(GraphConnector)getGraphNode().getAnchorage().get(0)).eAdapters().remove(graphElementListener);
		}
		//if this is a icon figure we have to dispose associated image
		super.deactivate();
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals() {

		/** Stereotype presentation */
		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p ,size);

		IFigure fig = getFigure();

		if( fig instanceof RequiredInterfaceFigure){
			if(getGraphNode().getAnchorage().size()!=0){
				GraphConnector gc=(GraphConnector)getGraphNode().getAnchorage().get(0);
				Rectangle insideRect=r.getCopy().crop(new Insets(5));
				int gcPos=insideRect.getPosition(gc.getPosition());
				if( gcPos==PositionConstants.NORTH){
					((RequiredInterfaceFigure)fig).setOrientation(PositionConstants.SOUTH);}
				if( gcPos==PositionConstants.SOUTH){
					((RequiredInterfaceFigure)fig).setOrientation(PositionConstants.NORTH);}
				if( gcPos==PositionConstants.EAST){
					((RequiredInterfaceFigure)fig).setOrientation(PositionConstants.WEST);}
				if( gcPos==PositionConstants.WEST){
					((RequiredInterfaceFigure)fig).setOrientation(PositionConstants.EAST);}
			}
		}
		((GraphicalEditPart) getParent()).setLayoutConstraint(
				this,
				fig,
				r);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#getUmlElement()
	 */
	@Override
	public org.eclipse.uml2.uml.Interface getUmlElement() {
		Element e=((Uml1SemanticModelBridge)getGraphNode().getSemanticModel()).getElement();
		if(e instanceof ElementImport) {//may be an Element import
			return (Interface)((ElementImport)e).getImportedElement();
		}
		return ((Interface)e);
	}
}
